package com.perfma.xtest.tool;

import com.google.common.collect.Lists;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.StringProperty;

import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * @author: junhui.si
 * @date: 2022-07-08 17:11
 **/
public class LastDemo {
    private static final Map<String, JMeterProperty> propMap =
            Collections.synchronizedMap(new LinkedHashMap<>());

    public static void main(String[] args) {
//        String name = "事务控制器2.事务控制器2_1.事务控制器2_1_1.HTTP请求2_1_1";
//        int index = name.lastIndexOf(".");
//        System.out.printf(name.substring(0, index));

//        System.out.printf(legalAssertSource("${sdf}"));

        for (int i = 10000; i > 1; i--) {
            propMap.put(UUID.randomUUID().toString(), new StringProperty("" + i, i + "_"));
        }

//        for (int i = 0; i < 10; i++) {

            new Thread(() -> {
                remove();
            });

            new Thread(() -> {
                remove();
            }).start();
            ;

            new Thread(() -> {
                remove();
            }).start();

            new Thread(() -> {
                remove();
            }).start();
            ;

            new Thread(() -> {
                remove();
            }).start();


            new Thread(() -> {
                remove();
            }).start();
//        }
    }

    private static String legalAssertSource(String source) {
        if (source == null) {
            return null;
        }
        if (source.startsWith("${") && source.endsWith("}")) {
            return source.substring(2, source.length() - 1);
        }
        return source;
    }

    private static void remove() {
        Iterator<Map.Entry<String, JMeterProperty>> iter = propMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, JMeterProperty> entry = iter.next();
            Random r = new Random();
            String str = r.nextInt(9) + "";
            if (entry.getKey().contains(str)) {
                System.out.println(str + "          " + entry.getKey());
                iter.remove();
            }
        }
        System.out.println("##########################" + propMap.size());
    }
}
